package parkshop.pmcourse.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import parkshop.pmcourse.domain.Customer;
import parkshop.pmcourse.domain.Product;
import parkshop.pmcourse.domain.Shop;
import parkshop.pmcourse.domain.ShopOwner;
import parkshop.pmcourse.dto.PageBean;
import parkshop.pmcourse.mapping.CustomerMapper;
import parkshop.pmcourse.mapping.ProductMapper;
import parkshop.pmcourse.mapping.ShopMapper;
import parkshop.pmcourse.mapping.ShopOwnerMapper;
import parkshop.pmcourse.service.SearchService;
import parkshop.pmcourse.utils.ConstantUtil;

@Service
public class SearchServiceImpl implements SearchService{

	@Autowired
	private ProductMapper productMapper;
	@Autowired
	private CustomerMapper customerMapper;
	@Autowired
	private ShopMapper shopMapper;
	@Autowired
	private ShopOwnerMapper shopOwnerMapper;
	
	@Override
	public Product searchProductById(int id) {
		// TODO Auto-generated method stub
		return productMapper.selectByPrimaryKey((long) id);
	}
	@Override
	public List<Product> searchProductByName(String name) {
		// TODO Auto-generated method stub
		return productMapper.selectProductsByName(name);
	}

	@Override
	public PageBean searchPageProductByName(String name,int pageNumber) {
		// TODO Auto-generated method stub
		PageBean pageBean=new PageBean();
		pageBean.setType(name);
		pageBean.setStartindex((pageNumber-1)*ConstantUtil.pageSize);
		pageBean.setPageSize(ConstantUtil.pageSize);
		List<Product> productList=productMapper.selectProductsPageByName(pageBean);
		pageBean.setRecordList(productList);
		pageBean.setCurrentPage(pageNumber);
		int count=productMapper.selectProductsCountByName(name);
		pageBean.setRecordCount(count);
		pageBean.setPageCount(count%ConstantUtil.pageSize==0?count/ConstantUtil.pageSize:count/ConstantUtil.pageSize+1);
		return pageBean;
	}
	@Override
	public Customer searchCustomerById(int id) {
		// TODO Auto-generated method stub
		return customerMapper.selectByPrimaryKey((long) id);
	}

	@Override
	public Customer searchCustomerByName(String name) {
		// TODO Auto-generated method stub
		return customerMapper.selectByName(name);
	}

	@Override
	public Shop searchShopById(int id) {
		// TODO Auto-generated method stub
		return shopMapper.selectByPrimaryKey((long) id);
	}

	@Override
	public List<Shop> searchShopByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ShopOwner searchShopOwnerById(int id) {
		// TODO Auto-generated method stub
		return shopOwnerMapper.selectByPrimaryKey((long) id);
	}

	@Override
	public ShopOwner searchShopOwnerByName(String name) {
		// TODO Auto-generated method stub
		return shopOwnerMapper.selectByName(name);
	}
	

	

}
